<template>
  <div class="app-container" style="padding: 5px 0 0 0">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <el-row>
        <el-form-item label="卡券编号" prop="cardNo">
          <el-input v-model="queryParams.cardNo" placeholder="请输入卡券编号" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="卡券密码" prop="cardSecret">
          <el-input v-model="queryParams.cardSecret" placeholder="请输入卡券密码" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="客户昵称" prop="memberNickName">
          <el-input v-model="queryParams.memberNickName" placeholder="请输入客户昵称" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="兑换时间" prop="createTimeStr">
          <el-date-picker v-model="queryParams.createTimeStr" type="daterange" align="right" value-format="yyyy-MM-dd"
            unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 232px"
            :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item style="margin-left: 18px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>

    </el-form>
    <el-row class="row-mbinfo" style="margin-left: 0px; margin-right: 0px;">
      <el-row class="mb8">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-row>

    <div class="divTable">
      <el-table border v-loading="loading" :data="exchangeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="客户头像" align="center" width="100" prop="memberHeadImg" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-image v-if="scope.row.memberHeadImg != null && scope.row.memberHeadImg != ''" slot="reference"
              :src="scope.row.memberHeadImg" class="image-rows" :preview-src-list="[scope.row.memberHeadImg]"></el-image>
          </template>
        </el-table-column>

        <el-table-column label="客户昵称" align="center" width="180" prop="memberNickName" :show-overflow-tooltip="true" />
        <el-table-column label="兑换编号" width="240" align="center" prop="cardNo" :show-overflow-tooltip="true" />
        <el-table-column label="商品类型" width="150" align="center" prop="cardTypeStr" :show-overflow-tooltip="true" />
        <el-table-column label="商品图片" align="center" width="100" prop="objectImage" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-image v-if="scope.row.objectImage != null && scope.row.objectImage != ''" slot="reference"
              :src="scope.row.objectImage" class="image-rows" :preview-src-list="[scope.row.objectImage]"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" align="center" min-width="200" prop="objectName" :show-overflow-tooltip="true" />
        <el-table-column label="sku" align="center" prop="skuName" :show-overflow-tooltip="true" />
        <el-table-column label="卡券密码" width="100" align="center" prop="cardSecret" :show-overflow-tooltip="true" />
        <el-table-column label="数据状态" width="100" align="center" prop="totalAmount" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>
              {{ scope.row.getStatus == '1' ? '已兑换' : '待兑换' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="兑换时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改兑换记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看兑换记录对话框 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="700px" append-to-body>
      <el-form ref="detailForm" label-width="100px">
        <el-row>
          <el-col :span="12" v-for="(item, index) in showData" :key="index" style="margin-top:-15px">
            <el-form-item :label="`${item.label}：`">
              <span>{{ item.value }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="handCloseDetail">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listExchange,
  getExchange,
  delExchange,
  addExchange,
  updateExchange,
  updateDataStatus
} from "@/api/activity/exchange";

export default {
  name: "Exchange",
  components: {},
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 兑换记录表格数据
      exchangeList: [],
      // 弹出层标题
      title: "",
      //查看详细标题
      viewTitle: "",
      // 是否显示弹出层
      open: false,
      //是否显示查看详细弹出层
      viewOpen: false,
      //查看详细数据
      showData: [],
      // 数据状态字典
      dataStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberNickName: null,
        memberHeadImg: null,
        cardId: null,
        cardNo: null,
        cardAmount: null,
        cardName: null,
        phone: null,
        cardSecret: null,
        dataStatus: null,
        createUser: null,
        createTime: null,
        createTimeStr: null,
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      columns: [
        {
          label: '兑换ID',
          dataIndex: 'exchangeId',
        },
        {
          label: '会员ID',
          dataIndex: 'memberUid',
        },
        {
          label: '兑换客户昵称',
          dataIndex: 'memberNickName',
        },
        {
          label: '兑换客户头像',
          dataIndex: 'memberHeadImg',
        },
        {
          label: '兑换卡ID',
          dataIndex: 'cardId',
        },
        {
          label: '兑换卡编号',
          dataIndex: 'cardNo',
        },
        {
          label: '授信金额',
          dataIndex: 'cardAmount',
        },
        {
          label: '持卡人姓名',
          dataIndex: 'cardName',
        },
        {
          label: '兑换时留下的电话号码',
          dataIndex: 'phone',
        },
        {
          label: '卡券密码',
          dataIndex: 'cardSecret',
        },
        {
          label: '卡券类型',
          dataIndex: 'cardType',
        },
        {
          label: '数据状态',
          dataIndex: 'dataStatus',
        },
        {
          label: '客户编号',
          dataIndex: 'customerUid',
        },
        {
          label: '客户名称',
          dataIndex: 'customerName',
        },
        {
          label: '兑换人',
          dataIndex: 'createUser',
        },
        {
          label: '兑换时间',
          dataIndex: 'createTime',
        },
        {
          label: '修改人',
          dataIndex: 'updateUser',
        },
        {
          label: '修改时间',
          dataIndex: 'updateTime',
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created () {
    this.getList();
    this.getDicts("activity_coupon_state").then(response => {
      this.dataStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询兑换记录列表 */
    getList () {
      this.loading = true;
      listExchange(this.queryParams).then(response => {
        this.exchangeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 数据状态字典翻译
    dataStatusFormat (row, column) {
      return this.selectDictLabel(this.dataStatusOptions, row.dataStatus);
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        exchangeId: null,
        memberUid: null,
        memberNickName: null,
        memberHeadImg: null,
        cardId: null,
        cardNo: null,
        cardAmount: null,
        cardName: null,
        phone: null,
        cardSecret: null,
        cardType: null,
        dataStatus: null,
        customerUid: null,
        customerName: null,
        createUser: null,
        createTime: null,
        updateUser: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.exchangeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加兑换记录";
    },
    /** 查看详情按钮操作 **/
    handleShowDetail (row) {
      this.viewOpen = true;
      this.viewTitle = "查看兑换记录";
      this.showData = [];
      this.columns.forEach(item => {
        this.showData.push({
          label: item.label,
          value: row[item.dataIndex]
        })
      })
    },
    handCloseDetail () {
      this.viewOpen = false
      this.showData = []
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const exchangeId = row.exchangeId || this.ids
      getExchange(exchangeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改兑换记录";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.exchangeId != null) {
            updateExchange(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExchange(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const exchangeIds = row.exchangeId || this.ids;
      this.$confirm('是否确认删除兑换记录编号为"' + exchangeIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delExchange(exchangeIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('activity/exchange/export', {
        ...this.queryParams
      }, `activity_exchange.xlsx`)
    },
    // 状态修改
    handleStatusChange (row) {
      let text = row.dataStatus === '0' ? '无效' : '有效'
      this.$confirm(
        '确定要把批次编号："' + row.cardNo + '"设为' + text + '吗？',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function () {
          return updateDataStatus(row.exchangeId, row.dataStatus)
        })
        .then(() => {
          this.msgSuccess('操作成功')
          this.getList()
        })
        .catch(function () {
          row.dataStatus = row.dataStatus === '0' ? '1' : '0'
        })
    },
  }
};
</script>
